package view.chart;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.Box;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 *
 * @author Kevin
 */
public class PieChart extends Chart
{
    private JPanel legend;
    
    public PieChart(ChartValue[] values)
    {
        super(values);
        
        if (values != null)
            createLegend();
    }
    
    public PieChart()
    {
        this(null);
    }

    @Override
    public void setValues(ChartValue[] values)
    {
        super.setValues(values);
        
        setLayout(new BorderLayout());
        
        if (values != null)
            createLegend();
    }

    @Override
    protected void paintChart(Graphics2D g2d)
    {
        ChartValue[] values = getValues();
        
        if (values == null)
            return;
        
        double total = 0.0D;
        for (ChartValue cv : values)
            total += cv.getValue();

        // Minor hack to make room for the legend
        int w = getWidth() - 200;
        int h = getHeight();
        
        int drawSize = Math.min(w, h);
        
        int x = w / 4;
        int y = h / 8;
        
        g2d.setRenderingHint(
                RenderingHints.KEY_ANTIALIASING, 
                RenderingHints.VALUE_ANTIALIAS_ON
            );
        
        int currValue = 0;
        for (int i = 0, len = values.length; i < len; i++)
        {
            ChartValue cv = values[i];
            
            int startAngle = (int) Math.ceil(currValue * 360 / total);
            int arcAngle = (int) Math.ceil(cv.getValue() * 360 / total);
            
            // If we're at the last piece of the pie
            // we need to make sure that the piece stretches to
            // the last last piece of the pie
            if (i == len - 1)
                arcAngle = 360 - startAngle;
            
            g2d.setColor(cv.getColor());
            g2d.fillArc(x, y, drawSize, drawSize, startAngle, arcAngle);
            
            currValue += cv.getValue();
        }
    }
    
    private void createLegend()
    {
        if (legend == null)
        {
            legend = new JPanel();
            legend.setLayout(new FlowLayout(FlowLayout.LEFT));
            legend.setOpaque(false);
            
            add(legend);
        }
        
        FontMetrics fontMetrics = getFontMetrics(getFont());
        
        legend.removeAll();
        
        int panelWidth = 0;
        for (ChartValue cv : getValues())
        {
            if (fontMetrics.stringWidth(cv.getLabel()) > panelWidth)
                panelWidth = fontMetrics.stringWidth(cv.getLabel());
            
            JPanel coloredSquare = new JPanel();
            coloredSquare.setPreferredSize(new Dimension(8, 8));
            coloredSquare.setBackground(cv.getColor());

            Box legendItem = Box.createHorizontalBox();
            legendItem.add(coloredSquare);
            legendItem.add(Box.createHorizontalStrut(4));
            legendItem.add(new JLabel(cv.getLabel()));

            legend.add(legendItem);
        }
        
        legend.setPreferredSize(new Dimension(panelWidth, 0));
        legend.revalidate();
    }
}
